package com.concerneyes.health;

import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.health.Task.GlassManDataImpl;
import com.concerneyes.health.Task.GlassManDataListener;
import com.concerneyes.health.adapter.HealthAdapter;
import com.concerneyes.health.adapter.HealthDailogAdapter;
import com.concerneyes.health.model.HealthDataModel;
import com.concerneyes.mine.Task.GlassManImpl;
import com.concerneyes.mine.Task.GlassManListener;
import com.concerneyes.mine.model.MyGlassManModel;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import common.AbsActivity;
import util.ActivityUtil;
import util.Util;
import view.ListViewForScrollView;
import view.MyUniversalDialog;


/**
 * 健康View
 */
public class HealthActivity extends AbsActivity implements View.OnClickListener
        , HealthAdapter.OnHealthItemClick, GlassManListener, GlassManDataListener {
    private ListViewForScrollView mLvOptometry;
    private HealthAdapter adapter;

    private TextView mEditOptometry;
    private TextView mOptometryDate;
    private TextView mDataCome;
    private LinearLayout mLlHisData;
    private MyUniversalDialog dialog;
    private GlassManImpl impl;
    private GlassManDataImpl dataImpl;
    private MyGlassManModel manModel;
    private HealthDailogAdapter dailogAdapter;
    private TextView mJob;
    private TextView mAge;
    private TextView mDrive;
    private TextView mComputer;
    private TextView mProposal;
    private boolean isUser;
    private ScrollView slHealth;
    private LinearLayout ll_top_view;
    private LinearLayout ll_optometry_single;
    private View view_1;
    private String manId = null;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_health);
        isUser = getIntent().getExtras().getBoolean("isUser");
        if (!isUser) {
            Util.setTriangle(this,getTitleView());
        } else {
            manId = getIntent().getExtras().getString("manId");
        }
        getRightText().setVisibility(View.VISIBLE);
        getRightText().setText(R.string.build_order);
        initView();
    }

    @Override
    public void onRightTextAction(View v) {
        super.onRightTextAction(v);
        ActivityUtil.setIntentForEdit(this, EditOptometryData.class
                , String.valueOf(manModel.getId()),
                EditOptometryData.IS_ADD, null);
    }

    private void initView() {
        mLvOptometry = (ListViewForScrollView) findViewById(R.id.lv_optometry);
        mLvOptometry.setPadding(0, 0, 0, 0);
        adapter = new HealthAdapter(this, this, false,true);
        mLvOptometry.setAdapter(adapter);
        mEditOptometry = (TextView) findViewById(R.id.edit_optometry);
        mEditOptometry.setOnClickListener(this);
        mOptometryDate = (TextView) findViewById(R.id.optometry_date);
        mDataCome = (TextView) findViewById(R.id.data_come);
        mLlHisData = (LinearLayout) findViewById(R.id.ll_his_data);
        mLlHisData.setOnClickListener(this);
        impl = new GlassManImpl(this, this);
        impl.listGlassMan();
        dataImpl = new GlassManDataImpl(this, this);
        mJob = (TextView) findViewById(R.id.job);
        mAge = (TextView) findViewById(R.id.age);
        mDrive = (TextView) findViewById(R.id.drive);
        mComputer = (TextView) findViewById(R.id.computer);
        mProposal = (TextView) findViewById(R.id.proposal);

        view_1 = findViewById(R.id.view_1);
        slHealth = (ScrollView) findViewById(R.id.sl_health);
        ll_top_view = (LinearLayout) findViewById(R.id.ll_top_view);
        ll_optometry_single = (LinearLayout) findViewById(R.id.ll_optometry_single);
        if (isUser) {
            ll_top_view.setVisibility(View.GONE);
            view_1.setVisibility(View.GONE);
            slHealth.setBackgroundColor(getResources().getColor(R.color.app_sky_blue));
            ll_optometry_single.setBackground(getResources().getDrawable(R.drawable.btn_gray_bold_bg));
            mLlHisData.setBackground(getResources().getDrawable(R.drawable.btn_gray_bold_bg));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        impl.listGlassMan();
//        dataImpl.listGlassManData(String.valueOf(manModel.getId()));
    }

    private void initDialog(List<MyGlassManModel> data) {
        int width = this.getWindowManager().getDefaultDisplay().getWidth();
        dialog = new MyUniversalDialog(this);
        dialog.setLayout(LayoutInflater.from(this).inflate(R.layout.dialog_health, null),
                MyUniversalDialog.DialogGravity.CENTERTOP, -2, width);
        dialog.setCanceledOnTouchOutside(true);

        ListView lvHealth = (ListView) dialog.findViewById(R.id.lv_dialog_health);
        View footerView = LayoutInflater.from(this).inflate(R.layout.health_dialog_item, null);
        lvHealth.addFooterView(footerView);
        dailogAdapter = new HealthDailogAdapter(this, data);
        lvHealth.setAdapter(dailogAdapter);

        RelativeLayout rl_health = (RelativeLayout) footerView.findViewById(R.id.rl_health);
        final TextView tv = (TextView) footerView.findViewById(R.id.tv);
        tv.setText(HealthActivity.this.getResources().getString(R.string.add_user));
        Drawable Left = getResources().getDrawable(R.drawable.ic_add);
        Left.setBounds(0, 0, ActivityUtil.dip2px(15), ActivityUtil.dip2px(15));
        tv.setCompoundDrawables(Left, null, null, null);
        tv.setCompoundDrawablePadding(ActivityUtil.dip2px(4));
        rl_health.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("isAdd", true);
                ActivityUtil.setIntent(HealthActivity.this, HealthAddUserActivity.class, bundle);
                dialog.dismiss();
            }
        });
        lvHealth.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyGlassManModel item = dailogAdapter.getItem(position);
                HealthActivity.this.setTitle(item.getRealName());
                setData(item);
                dialog.dismiss();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edit_optometry:
                ActivityUtil.setIntentForEdit(this, EditOptometryData.class
                        , String.valueOf(manModel.getId()),
                        EditOptometryData.IS_EDIT, null);
                break;
            case R.id.ll_his_data:
                Bundle bundle = new Bundle();
                bundle.putString("manId", String.valueOf(manModel.getId()));
                ActivityUtil.setIntent(this, HisDataActivity.class, bundle);
                break;
        }
    }

    @Override
    public void onTitleAction(View v) {
        super.onTitleAction(v);
        getTitleText().setVisibility(View.GONE);
        dialog.show();
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                getTitleText().setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void healthItemClick(int position, int textPosition, View v) {

    }

    @Override
    public void glassManListSuccess(List<MyGlassManModel> model) {
        initDialog(model);
        int position = 0;
        for (int i = 0; i < model.size(); i++) {
            if (manId != null && manId.equals(String.valueOf(model.get(i).getId()))) {
                position = i;
                break;
            }
        }
        setData(model.get(position));
    }

    private void setData(MyGlassManModel model) {
        this.manModel = model;
        setTitle(model.getRealName());
        mJob.setText(model.getJob());
        Calendar mycalendar = Calendar.getInstance();//获取现在时间
        int nowyear = Integer.parseInt(String.valueOf(mycalendar.get(Calendar.YEAR)));//获取年份
        mAge.setText(String.valueOf(nowyear - Integer.parseInt(model.getYear())) + "岁");
        mDrive.setText(model.getDriveFrequency());
        if (model.isIsUseComputer()) {
            mComputer.setText(getResources().getString(R.string.often));
        } else {
            mComputer.setText(getResources().getString(R.string.un_often));
        }
        mProposal.setText(getResources().getString(R.string.proposal));
        dataImpl.listGlassManData(String.valueOf(model.getId()));
    }

    @Override
    public void glassManListFail() {

    }

    @Override
    public void deleteGlassManSuccess(List<MyGlassManModel> model) {

    }

    @Override
    public void deleteGlassManFail() {

    }

    @Override
    public void addGlassManSuccess() {

    }

    @Override
    public void addGlassManFail() {
    }

    //-----------------------
    @Override
    public void glassManDataListSuccess(List<HealthDataModel> model) {
        adapter.setHealthData(model.get(0));
        mLvOptometry.setAdapter(adapter);
        String date = Util.changeStringDataStyle(model.get(0).getDataTime());
        mOptometryDate.setText(date);
        mDataCome.setText(model.get(0).getFrom());
    }

    @Override
    public void glassManDataListFail() {
        adapter.setHealthData(new HealthDataModel());
        mLvOptometry.setAdapter(adapter);
        mOptometryDate.setText("");
        mDataCome.setText("");
    }

    @Override
    public void deleteGlassManDataSuccess(List<HealthDataModel> model) {

    }

    @Override
    public void deleteGlassManDataFail() {

    }

    @Override
    public void addGlassManDataSuccess() {

    }

    @Override
    public void addGlassManDataFail() {

    }
}
